#pragma once

#include <ddblk/locks/syslock_int.h>

#include <pthread.h>

typedef struct _pspinlock
{
	pthread_spinlock_t lock;
} _pspinlock_t;

DECL_SYSLOCK(pspin)

PARAM_NONULL(1) SAI _pspinlock_t *_pspin_init(void *ptr, size_t size)
{	
	if (size < sizeof(_pspinlock_t)) return NULL;
	_pspinlock_t *s = (_pspinlock_t *)ptr;
	pthread_spin_init(&s->lock, PTHREAD_PROCESS_PRIVATE);
	return s;
}

PARAM_NONULL(1) SAI void *_pspin_destruct(_pspinlock_t *s)
{
	PARAM_USED(s);
	pthread_spin_destroy(&s->lock);
	return (void *)s;
}

PARAM_NONULL(1) SAI bool_t _pspin_lock(_pspinlock_t *s)
{
	return (!pthread_spin_lock(&s->lock));
}

PARAM_NONULL(1) SAI bool_t _pspin_unlock(_pspinlock_t *s)
{
	return (!pthread_spin_unlock(&s->lock));
}

DEF_SYSLOCK(pspin, pspin)
